<?php
//validator formulare pro obnoveni hesla - jestli ten hash existuje

require_once 'Zend/Validate/Abstract.php';

class Validate_IpCheck extends Zend_Validate_Abstract {
    const INVALID = 'IpDoesNotMatch';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID => "Your IP adress does not match the IP adress in database table.",
    );

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value contains only hexadecimal digit characters
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from('forgot','ip')
                               ->where("hash = ?", $value);
        $ip = $db->fetchOne($select);

        if ($ip == $_SERVER["REMOTE_ADDR"]) {
            return true;
        } else
        {
             $this->_error(self::INVALID);
             return false;
        }
    }

}
